<?php require_once("../includes/initializeSession.php"); ?>
<?php
if (!isset($_SESSION['admin'])) {
    header('Location: adminLogin.php');
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php require_once("../includes/cssAndJsAdmin.php"); ?>
<script type="text/javascript">
    $(document).ready(function() {




        $("#list").jqGrid({
            url: "../gridFunctions/personActions.php",
            editurl: "../gridFunctions/personActions.php",
            direction: 'rtl',
            async: false,
            datatype: 'json',
            pager: '#pager',
            // colNames: ['שורה','פעולות','מספר לקוח','ת"ז', 'שם פרטי', 'שם משפחה','מייל', 'עיר', 'רחוב ובית', 'טלפון', 'פקס', 'פלאפון'],
            colNames: ['מספר לקוח', 'ת"ז', 'שם פרטי', 'שם משפחה', 'מייל', 'עיר', 'רחוב ובית', 'טלפון', 'פקס', 'פלאפון'],
            colModel: [
                //	{name:'rowNumber',index:'rowNumber', width:20,sortable:false,search : false},
                //{name:'act',index:'act', width:75,sortable:false,search : false},
                {name: 'person_number', index: 'person_number', width: 120, editable: true, editoptions: {readonly: "readonly"}, align: 'right'},
                {name: 'person_id', index: 'person_id', width: 60, editable: true, align: 'right', editrules: {required: true, integer: true}},
                {name: 'firstname', index: 'firstname', width: 120, editable: true, align: 'right', editrules: {required: true}},
                {name: 'lastname', index: 'lastname', width: 120, editable: true, align: 'right', editrules: {required: true}},
                {name: 'email', index: 'email', width: 120, editable: true, align: 'right', editrules: {email: true}},
                {name: 'city', index: 'city', width: 70, editable: true, align: 'right', editrules: {required: true}},
                {name: 'address', index: 'address', width: 70, editable: true, align: 'right', editrules: {required: true}},
                {name: 'telephone', index: 'telephone', width: 80, editable: true, align: 'right', editrules: {custom: true, custom_func: phoneCalidtion}},
                {name: 'fax', index: 'fax', width: 60, editable: true, align: 'right', editrules: {custom: true, custom_func: phoneCalidtion}},
                {name: 'cellphone', index: 'cellphone', width: 180, editable: true, align: 'right', editrules: {custom: true, custom_func: phoneCalidtion}}],
            pager: jQuery('#pager'),
                    rowNum: 20,
            rowList: [5, 10, 20, 50],
            sortname: 'person_number',
            sortorder: "desc",
            /*  gridComplete: function()
             {
             //jQuery("#grid_id").editRow(rowid, keys, oneditfunc, successfunc, url, extraparam, aftersavefunc,errorfunc, afterrestorefunc);
             //jQuery("#grid_id").saveRow(rowid, successfunc, url, extraparam, aftersavefunc,errorfunc, afterrestorefunc);
             ------	editparameters = {
             "keys" : false,
             "oneditfunc" : null,
             "successfunc" : null,
             "url" : null,
             "extraparam" : {},
             "aftersavefunc" : null,
             "errorfunc": null,
             "afterrestorefunc" : null,
             "restoreAfterError" : true,
             "mtype" : "POST"
             }

             saveparameters = {
             "successfunc" : null,
             "url" : null,
             "extraparam" : {},
             "aftersavefunc" : null,
             "errorfunc": null,
             "afterrestorefunc" : null,
             "restoreAfterError" : true,
             "mtype" : "POST"
             }

             var ids = jQuery("#list").jqGrid('getDataIDs');

             for(var i=0;i < ids.length;i++){
             var cl = ids[i];
             be ="<img class='imgBTN' src='css/25EDIT.png' alt='ערוך' height='25' width='25' onclick=\"$('#list').jqGrid('editRow',$('.ui-state-hover').attr('id'),  editparameters);\"  />"
             se ="<img class='imgBTN' src='css/25SAVE.png' alt='שמור' height='25' width='25' onclick=\"$('#list').jqGrid('saveRow',$('.ui-state-hover').attr('id'),  saveparameters);\"  />"
             ce ="<img class='imgBTN' src='css/25CANCEL.png' alt='בטל' height='25' width='25' onclick=\"$('#list').restoreRow($('.ui-state-hover').attr('id'));\"  />"
             jQuery("#list").jqGrid('setRowData',ids[i],{act:be+se+ce});
             jQuery("#list").jqGrid('setRowData',ids[i],{rowNumber:i});
             }	
             }, // -- end of     gridComplete: function()-----*/
            mtype: "POST",
            //loadonce: true,
            postData: {oper: 'view'},
            viewrecords: true,
            imgpath: '../../../Scripts/css/Aristo/images',
            autowidth: true,
            height: "100%"
        }); // -- end of   $("#list").jqGrid({


        $("#list").jqGrid('navGrid', '#pager', {edit: true, add: true, del: true, refresh: true, closeAfterAdd: true, reloadAfterSubmit: true, search: false},
        // Edit dialog parameters
        {
            reloadAfterSubmit: true,
            closeAfterEdit: true,
            //editData: {person_number: function() {  return "7"; } },
            onclickSubmit: function(rp_ge, postdata)
            {
                return {person_number: getSelectedCell('person_number')};
            },
            afterSubmit: function(response, postdata)
            {
                if (response.responseText == "ok")
                {
                    success = true;
                    alert("פרטי הלקוח התעדכנו בהצלחה")
                }
                else
                {

                    success = false;
                    alert("פרטי הלקוח לא התעדכנו בהצלחה")
                }
                return [success, response.responseText]
            }
        },
        // Add dialog parameters
        {
            reloadAfterSubmit: true,
            closeAfterAdd: true,
            afterSubmit: function(response, postdata) {
                if (response.responseText == "ok")
                {
                    success = true;
                    alert("לקוח התווסף בהצלחה")
                }
                else
                {
                    success = false;
                    alert("לקוח לא התווסף בהצלחה")
                }
                return [success, response.responseText]
            }

        },
        // Delete dialog parameters
        {
            reloadAfterSubmit: true,
            onclickSubmit: function(rp_ge, postdata)
            {
                return {person_number: getSelectedCell('person_number')};
            },
            afterSubmit: function(response, postdata) {
                if (response.responseText == "ok")
                {
                    success = true;
                    alert("לקוח נמחק בהצלחה")
                }
                else
                {
                    success = false;
                    alert("לקוח לא נמחק בהצלחה")
                }
                return [success, response.responseText]
            }

        },
        // Search dialog parameters
        {},
        // View dialog parameters
        {}
        );
        $('#list').jqGrid('filterToolbar', {searchOnEnter: true, enableClear: false});
    });// -- end of  $(document).ready(function ()
    function getSelectedCell(str)
    {
        var grid = jQuery('#list');
        var sel_id = grid.jqGrid('getGridParam', 'selrow');
        var myCellData = grid.jqGrid('getCell', sel_id, str);
        return myCellData;
    }
</script>
<title>ניהול לקוחות</title>
</head>
<body dir="rtl"  style="padding:0px; margin:0px;">
    <div id="main">
        <?php include("adminHeader.php"); ?>
        <h1>רשימת לקוחות </h1>
        <div id="lc">
            <table id="list" class="scroll" cellpadding="0" cellspacing="0">
            </table>
            <div id="pager" class="scroll" style="text-align: center;"> </div>
        </div>
        <?php include("../footer.php"); ?>	
    </div>
</body>
</html>